var mysql=require('mysql');
var crypto=require('./crypto');

var pool=null;

function query(sql,callback){
    pool.getConnection(function(err,conn){
        if(err){
            callback(err,null,null);
        }else{
            conn.query(sql,function(qerr,vals,fields){
                conn.release();
                callback(qerr,vals,fields);
            });
        }
    });
}
exports.query = query;

function nop(a,b,c,d,e,f,g){
}

exports.init=function(config){
    pool=mysql.createPool({
        host:config.host,
        user:config.user,
        password:config.password,
        database:config.db,
        port:config.port
    });
};

exports.is_account_exist=function(account,callback){
    callback=callback==null?nop:callback;
    if(account==null){
        callback(0);
        return;
    }
    var sql='select * from accounts where account = "{0}"';
    sql=sql.format(account);
    query(sql,function(err,rows,fields){
        if(err){
            callback(1);
            throw err;
        }else{
            if(rows.length>0){
                callback(2);
            }else{
                callback(3);
            }
        }
    });
};

exports.create_account=function(account,password,daili_id,realname,nickname,head_index,score,callback){
    callback=callback==null?nop:callback;
    if(account==null||password==null||account==null||daili_id==null||realname==null){
        callback(false);
        return;
    }
    this.create_user(account,daili_id,realname,nickname,head_index,score,function(agr){
        if(agr){
            var sql='insert into accounts(account,password) values("{0}","{1}")';
            sql=sql.format(account,password);
            query(sql,function(err,rows,fields){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }else{
            callback(false);
        }
    });
    
};

exports.create_user=function(account,daili_id,realname,nickname,head_index,score,callback){
    callback=callback==null?nop:callback;
    if(account==null||daili_id==null||realname==null)
    {
        callback(false);
        return;
    }
    if(nickname==''){
        nickname=account;
    }
    if(head_index==null){
        head_index=0;
    }
    if(score==null){
        score=0;
    }
    var sql='insert into users(account,daili_id,realname,nickname,head_index,score) VALUES("{0}","{1}","{2}","{3}","{4}",{5})';
    sql=sql.format(account,daili_id,realname,nickname,head_index,score);
    console.log(sql);
    query(sql,function(err,rows,fields){
        if (err) {
            callback(false);
            throw err;
        }
        callback(true);
    });
};

exports.login_account=function(account,password,callback){
    callback=callback==null?nop:callback;
    if(account==null||password==null){
        callback(false);
        return;
    }
    var sql='select account,password from accounts WHERE account="{0}"';
    sql=sql.format(account);
    query(sql,function(err,rows,fields){
        if(err){
            callback(false);
            throw err;
        }
        
        if(rows.length>0&&rows[0].password==password){
            callback(true);
        }else{
            callback(false);
        }
    });
};

exports.get_user_info=function(account,callback){
    callback=callback==null?nop:callback;
    if(account==null){
        callback(false);
        return;
    }
    var sql='select * from users WHERE account="{0}"';
    sql=sql.format(account);
    query(sql,function(err,rows,fields){
        if(err){
            callback(false);
            throw err;
        }
        if(rows.length>0){
            callback(true,rows[0]);
        }else{
            callback(false);
        }
    });
};

exports.update_user_lasttime=function(account,lasttime,callback){
    callback=callback==null?nop:callback;
    if(account==null){
        callback(false);
        return;
    }
    var sql = 'UPDATE users SET lasttime="{0}" WHERE account="{1}"';
    sql = sql.format(lasttime,account);
    console.log(sql);
    query(sql, function(err,rows,fields) {
        if (err) {
            callback(false);
            throw err;
        }
        callback(true);
    });
};

exports.get_game_list=function(callback){
    callback=callback==null?nop:callback;
    var sql='select * from games order by typeid';
    query(sql,function(err,rows,fields){
        if(err){
            callback(false);
            throw err;
        }
        if(rows.length>0){
            callback(true,rows);
        }else{
            callback(false);
        }
    });
};

exports.get_gamegroup_list=function(callback){
    callback=callback==null?nop:callback;
    var sql='select * from game_group';
    query(sql,function(err,rows,fields){
        if(err){
            callback(false);
            throw err;
        }
        if(rows.length>0){
            callback(true,rows);
        }else{
            callback(false);
        }
    });
};

exports.get_games_recommend=function(callback){
    callback=callback==null?nop:callback;
    var sql='select id from games where retype=1';
    query(sql,function(err,rows,fields){
        if(err){
            callback(false);
            throw err;
        }
        if(rows.length>0){
            callback(true,rows);
        }else{
            callback(false);
        }
    });
};

exports.get_notices_public=function(callback){
    callback=callback==null?nop:callback;
    var sql='select * from notices';
    query(sql,function(err,rows,fields){
        if(err){
            callback(false);
            throw err;
        }
        if(rows.length>0){
            callback(true,rows);
        }else{
            callback(false);
        }
    });
};
exports.update_UseSaveLoveGames=function(account,lovegame,callback){
    callback=callback==null?nop:callback;
    if(account==null){
        callback(false);
        return;
    }
    var sql="UPDATE users SET lovegame='{0}' WHERE account='{1}'";
    sql=sql.format(lovegame,account);
    console.log(sql);
    query(sql,function(err,rows,fields){
        if(err){
            callback(false);
            throw err;
        }
        callback(true);
    });
};

